package n28;

public class Main {
    public static int strStr(String haystack, String needle) {
        if (haystack == null || needle == null || haystack.length() == 0 || needle.length() == 0) {
            return -1;
        }
        if (needle.length()>haystack.length()){
            return -1;
        }
        char[] haystackChar = haystack.toCharArray();
        char[] needleChar = needle.toCharArray();
        for (int i = 0; i < haystackChar.length; i++) {

            int j = 0;
            for (; j < needleChar.length; j++) {
                if (i+j>haystack.length()-1){
                    return -1;
                }
                if (haystackChar[i+j] != needleChar[j]) {
                    break;
                }
            }
            if (j == needleChar.length) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        // System.out.println(strStr("leetcode", "leeto"));
        // System.out.println(strStr("sadbutsad", "sad"));
        // System.out.println(strStr("sadbutsad", "but"));
        // System.out.println(strStr("aaa", "aaaa"));
        System.out.println(strStr("mississippi", "issipi"));
    }
}
